package com.yc.jpyy.weekcheck.service.imp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.weekcheck.dao.WeekCheckDao;
import com.yc.jpyy.weekcheck.service.WeekCheckService;

@Service
public class WeekCheckServiceImpl extends BaseService implements WeekCheckService {
 

@Autowired
private WeekCheckDao weekCheckDao;

@Override
public Map<String, Object> selectWeekCheckById(Map<String, Object> paramsMap) throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	Map<String,Object> weekInfo = weekCheckDao.selectWeekCheckById(paramsMap);
	if(weekInfo != null){
		resultMap.put("returnCode", 1);
		resultMap.put("ListData", weekInfo);
		resultMap.put("returnMsg", "操作成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "系统查询不到该条信息");
	}
	return resultMap;
}

@Override
public Map<String, Object> addWeekCheck(Map<String, Object> paramsMap)
		throws DataAccessException, ParseException {
	Map<String,Object> resultMap = new HashMap<>();
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	//查询上次检查时间
	String lastCheckDate = weekCheckDao.queryCheckDate(paramsMap);
	if(lastCheckDate.equals("") || lastCheckDate == null){
		paramsMap.put("lastcheckdate", paramsMap.get("creatdate").toString());
	}else{
		paramsMap.put("lastcheckdate", lastCheckDate);
	}
	Date lastTime = sdf.parse(paramsMap.get("lastcheckdate").toString());
	Date checkTime = sdf.parse(paramsMap.get("creatdate").toString());
	/*java.time.Duration duration = java.time.Duration.between(lastTime,checkTime);
	long minutes = duration.toMinutes();
	//默认是1
	int intervalweeks = 0;
	if(minutes / 10080 > 0 && minutes % 10080 > 0){
		intervalweeks = (int) (minutes / 10080 + 1);
	}else if(minutes / 10080 > 0 && minutes % 10080 == 0){
		intervalweeks = (int) (minutes / 10080);
	}*/
	int intervalweeks = 0;
	//之间间隔的周数，相邻的两周结果是1，所以要减1
	int i = (int) ((checkTime.getTime() - lastTime.getTime())/86400000/7);
	if (i > 1){
		intervalweeks = i - 1;
	}
	paramsMap.put("intervalweeks", intervalweeks);
	int count = weekCheckDao.addWeekCheck(paramsMap);
	if(count <= 0){
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "新增失败");
	}else{
		resultMap.put("returnCode", 1);
		resultMap.put("returnMsg", "新增成功");
	}
	return resultMap;
}

@Override
public Map<String, Object> selectWeekCheckByPage(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
	int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
	paramsMap.put("pagestart", (pagenum-1) * pagesize);
	List<Map<String,Object>> weekCheckList = weekCheckDao.selectWeekCheckByPage(paramsMap);
	int weekCheckCounts = weekCheckDao.selectWeekCheckCount(paramsMap);
	if(weekCheckList != null && weekCheckList.size() > 0){
		resultMap.put("returnCode", 1);
		resultMap.put("ListData",weekCheckList);
		resultMap.put("TotalCount",weekCheckCounts);
		resultMap.put("TotalPage",weekCheckCounts % pagesize >0 ? weekCheckCounts/pagesize + 1 : weekCheckCounts/pagesize);
		resultMap.put("returnMsg", "操作成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "系统无数据");
	}
	return resultMap;
}

@Override
public Map<String, Object> delWeekCheck(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	paramsMap.put("ids", paramsMap.get("ids").toString().split(","));
	int count = weekCheckDao.delWeekCheck(paramsMap);
	if(count <= 0){
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "删除失败");
	}else{
		resultMap.put("returnCode", 1);
		resultMap.put("returnMsg", "删除成功");
	}
	return resultMap;
}

@Override
public Map<String, Object> addBatchWeekCheck(List<Map<String, Object>> dataList)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	dataList.stream().forEach(map->{
		//查询上次检查时间
		String lastCheckDate = weekCheckDao.queryCheckDate(map);
		if(lastCheckDate.equals("") || lastCheckDate == null){
			map.put("lastcheckdate", map.get("creatdate").toString());
		}else{
			map.put("lastcheckdate", lastCheckDate);
		}
		int intervalweeks = 0;
		try {
			Date lastTime = sdf.parse(map.get("lastcheckdate").toString());
			Date checkTime = sdf.parse(map.get("creatdate").toString());
			//之间间隔的周数，相邻的两周结果是1，所以要减1
			int i = (int) ((checkTime.getTime() - lastTime.getTime())/86400000/7);
			if (i > 1){
				intervalweeks = i - 1;
			}	
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.put("intervalweeks", intervalweeks);
	});
	int count = weekCheckDao.addBatchWeekCheck(dataList);
	if(count <= 0){
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "新增失败");
	}else{
		resultMap.put("returnCode", 1);
		resultMap.put("returnMsg", "新增成功");
	}
	return resultMap;
}

@Override
public Map<String, Object> selectWeekCheckByPages(Map<String, Object> paramsMap) throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
	int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
	paramsMap.put("pagestart", (pagenum-1) * pagesize);
	String companyid = Optional.ofNullable(paramsMap.get("companyid")).orElse("").toString();
	paramsMap.put("companyid", companyid.equals("")?"":companyid.split(","));
	List<Map<String,Object>> weekCheckList = weekCheckDao.selectWeekCheckByPages(paramsMap);
	int weekCheckCounts = weekCheckDao.selectWeekCheckCounts(paramsMap);
	if(weekCheckList != null && weekCheckList.size() > 0){
		resultMap.put("returnCode", 1);
		resultMap.put("ListData",weekCheckList);
		resultMap.put("TotalCount",weekCheckCounts);
		resultMap.put("TotalPage",weekCheckCounts % pagesize >0 ? weekCheckCounts/pagesize + 1 : weekCheckCounts/pagesize);
		resultMap.put("returnMsg", "操作成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "系统无数据");
	}
	return resultMap;
}

}
